package org.rednebula.framework.core.context;

import java.io.Serializable;

import org.jbpm.pvm.internal.processengine.ProcessEngineImpl;
import org.rednebula.framework.core.utils.InternalConstant;
import org.springframework.context.ApplicationContext;


public class FlowContext extends SpringContext implements Serializable {
	
	ProcessEngineImpl processEngineImpl;
	
	public FlowContext(ApplicationContext context) {
		super(context);
		this.setName(FLOW);
	}
	
	public void setProcessEngineImpl(ProcessEngineImpl processEngineImpl) {
		this.processEngineImpl = processEngineImpl;
	}


	public void close() {
		this.setContext(null);
	}
    private ProcessEngineImpl getProcessEngineImpl(){
    	if(processEngineImpl==null){
    		return (ProcessEngineImpl)getContext().getBean(InternalConstant.JBPM_PROCESSENGINE_NAME);
    	}
    	return processEngineImpl;
    }
	public <T> T get(String key) {
		if(isSelf(key))
			return (T)getProcessEngineImpl();
		return (T)getProcessEngineImpl().get(key);
	}

	public <T> T get(Class<T> type) {
		return (T)getProcessEngineImpl().get(type);
	}

	public boolean has(String name) {
		return this.isSelf(name)|| getProcessEngineImpl().has(name);
	}

}
